package com.info33.platform.config.rabbitMq;

import com.info33.platform.common.util.MQConstantUtil;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**系统队列配置
 * 主要定义默认交换机bean以及延迟消息相关队列
 */

@Configuration
public class SystemQueueConfiguration {

    /**
     * 默认及时消息交换机
     * @return
     */
    @Bean("defaultDirectExchange")
    public DirectExchange defaultDirectExchange() {
        return new DirectExchange(MQConstantUtil.DEFAULT_DIRECT_EXCHANGE_NAME, true, false);
    }

    /**
     * 配置默认延迟的 交换机
     */
    @Bean("defaultDelayedExchange")
    public CustomExchange defaultDelayedExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        //第二个参数是固定的  x-delayed-message
        return new CustomExchange(MQConstantUtil.DEFAULT_DELAYED_EXCHANGE, MQConstantUtil.DEFAULT_DELAYED_TYPE_NAME, true, false, args);
    }

    /**
     * 默认延迟消息接受并转发消息队列
     * @return
     */
    @Bean
    public Queue defaultRepeatTradeQueue() {
        return new Queue(MQConstantUtil.DEFAULT_REPEAT_TRADE_QUEUE_NAME,true,false,false);
    }

    /**
     * 建立延迟转发队列和交换机之间的关系
     * @return
     */
    @Bean
    public Binding defaultRepeatTradeBinding() {
        return BindingBuilder.bind(defaultRepeatTradeQueue()).to(defaultDelayedExchange()).with(MQConstantUtil.DEFAULT_REPEAT_TRADE_QUEUE_NAME).noargs();
    }
}
